<template>
  <div>
    <el-form ref="pram" :model="pram" :rules="rules" label-width="180px" @submit.native.prevent>
      <el-row>
        <el-col :span="8">
          <el-form-item label="型号名称" prop="name">
            <el-input v-model="pram.name" maxlength="100" placeholder="型号名称"/>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="所属单位" prop="departmentId">
            <el-cascader v-model="pram.departmentId" :disabled="pram.departmentId ===0" :options="parentOptions"
                         :props="categoryProps" style="width:100%"
            />
            <!-- allTreeList or parentOptions-->
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="作战使命" prop="operationalMission">
            <el-input v-model="pram.operationalMission" maxlength="100" placeholder="作战使命"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8">
          <el-form-item label="攻击目标" prop="attackTarget">
            <el-input v-model="pram.attackTarget" maxlength="100" placeholder="攻击目标"/>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="作战使用方式" prop="operationalMode">
            <el-input v-model="pram.operationalMode" maxlength="100" placeholder="作战使用方式"/>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="制导方式" prop="guidanceMode">
            <el-input v-model="pram.guidanceMode" maxlength="100" placeholder="制导方式"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8">
          <el-form-item label="适应载机" prop="adaptiveCarrier">
            <el-input v-model="pram.adaptiveCarrier" maxlength="100" placeholder="适应载机"/>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="发射条件" prop="launchingCondition">
            <el-input v-model="pram.launchingCondition" maxlength="100" placeholder="发射条件"/>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="发射方式" prop="LaunchingMode">
            <el-input v-model="pram.launchingMode" maxlength="100" placeholder="发射方式"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8">
          <el-form-item label="射程" prop="ranges">
            <el-input v-model="pram.ranges" maxlength="100" placeholder="射程"/>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="射速" prop="speed">
            <el-input v-model="pram.speed" maxlength="100" placeholder="射速"/>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="目标高度范围" prop="altitude">
            <el-input v-model="pram.altitude" maxlength="100" placeholder="目标高度范围"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8">
          <el-form-item label="命中精度" prop="hitAccuracy">
            <el-input v-model="pram.hitAccuracy" maxlength="100" placeholder="命中精度"/>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="末端约束条件" prop="restraint">
            <el-input v-model="pram.restraint" maxlength="100" placeholder="末端约束条件"/>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="RCS" prop="rcs">
            <el-input v-model="pram.rcs" maxlength="100" placeholder="RCS"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8">
          <el-form-item label="导引头抗干扰能力" prop="seeker">
            <el-input v-model="pram.seeker" maxlength="100" placeholder="导引头抗干扰能力"/>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="卫星定位系统抗干扰能力" prop="satellite">
            <el-input v-model="pram.satellite" maxlength="100" placeholder="卫星定位系统抗干扰能力"/>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="机动能力" prop="maneuver">
            <el-input v-model="pram.maneuver" maxlength="100" placeholder="机动能力"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8">
          <el-form-item label="目标适应性" prop="goal">
            <el-input v-model="pram.goal" maxlength="100" placeholder="目标适应性"/>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="引信" prop="fuze">
            <el-input v-model="pram.fuze" maxlength="100" placeholder="引信"/>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="战斗部" prop="warhead">
            <el-input v-model="pram.warhead" maxlength="100" placeholder="战斗部"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8">
          <el-form-item label="尺寸、重量" prop="dimension">
            <el-input v-model="pram.dimension" maxlength="100" placeholder="尺寸、重量"/>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="序号">
            <el-input-number v-model="pram.number" :min="0"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item>
        <el-button type="primary" @click="handlerSubmit('pram')"
                   v-hasPermi="['admin:model:type:update','admin:model:type:save']"
        >{{ isCreate === 0 ? '确定' : '更新' }}
        </el-button>
        <el-button @click="close">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import * as roleApi from '@/api/role.js'
import * as modelTypeApi from '@/api/modelType.js'
import { Debounce } from '@/utils/validate'

export default {
  // name: "edit"
  components: {},
  props: {
    isCreate: {
      type: Number,
      required: true
    },
    editData: {
      type: Object,
      default: () => {
        return { rules: [] }
      }
    },
    allTreeList: {
      type: Array
    },
    allCategoryData: {
      type: Object
    }
  },
  data() {
    /*const validatePhone = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('请填写手机号'));
      } else if (!/^1[3456789]\d{9}$/.test(value)) {
        callback(new Error('手机号格式不正确!'));
      } else {
        callback();
      }
    };*/
    return {
      constants: this.$constants,
      pram: {
        name: null,
        departmentId: null,
        operationalMission: null,
        attackTarget: null,
        operationalMode: null,

        guidanceMode: null,
        adaptiveCarrier: null,
        launchingCondition: null,
        launchingMode: null,
        ranges: null,
        speed: null,
        altitude: null,
        hitAccuracy: null,
        restraint: null,
        rcs: null,
        seeker: null,
        satellite: null,
        maneuver: null,
        goal: null,
        fuze: null,
        warhead: null,
        dimension: null,
        number: 0,
        id: 0
      },
      rules: {
        name: [{ required: true, message: '请填试验名称', trigger: ['blur', 'change'] }],
        departmentId: [{ required: true, message: '请选择所属单位', trigger: ['blur', 'change'] }]

      },
      categoryData: {
        modelTypeList: [],
        personList: []
      },
      parentOptions: [],
      categoryProps: {
        value: 'id',
        label: 'name',
        children: 'child',
        expandTrigger: 'hover',
        checkStrictly: false,
        emitPath: false
      }
    }
  },
  mounted() {
    this.initEditData()
  },
  methods: {
    close() {
      this.$emit('hideEditDialog')
    },
    initEditData() {
      this.categoryData = this.allCategoryData
      this.parentOptions = [...this.allTreeList]
      this.addTreeListLabelForCasCard(this.parentOptions, 'child')

      if (this.isCreate !== 1) return //编辑
      const {
        name,
        departmentId,
        operationalMission,
        attackTarget,
        operationalMode,
        guidanceMode,
        adaptiveCarrier,
        launchingCondition,
        launchingMode,
        ranges,
        speed,
        altitude,
        hitAccuracy,
        restraint,
        rcs,
        seeker,
        satellite,
        goal,
        fuze,
        warhead,
        dimension,
        maneuver,
        id,
        number
      } = this.editData //通过list中slot-scope=“scope“，scope.$index就是该行的下标，scope.row就是该行的数据所有消息对象，有了这两个参数就可以实现编辑、删除功能（this.editData就是scope.row传入的对象参数）
      this.pram.name = name//型号名称
      this.pram.departmentId = departmentId//所属单位
      this.pram.operationalMission = operationalMission//作战使命
      this.pram.attackTarget = attackTarget//攻击目标
      this.pram.operationalMode = operationalMode//作战使用方式
      this.pram.guidanceMode = guidanceMode//制导方式
      this.pram.adaptiveCarrier = adaptiveCarrier//适应载机
      this.pram.launchingCondition = launchingCondition//发射条件
      this.pram.launchingMode = launchingMode//发射方式
      this.pram.ranges = ranges//射程
      this.pram.speed = speed//速度
      this.pram.altitude = altitude//目标高度范围
      this.pram.hitAccuracy = hitAccuracy//命中精度
      this.pram.restraint = restraint//末端约束条件
      this.pram.rcs = rcs//RCS
      this.pram.seeker = seeker//导引头抗干扰能力
      this.pram.satellite = satellite//卫星定位系统抗干扰能力
      this.pram.maneuver = maneuver//机动能力
      this.pram.goal = goal//目标适应性
      this.pram.fuze = fuze//引信
      this.pram.warhead = warhead//战斗部
      this.pram.dimension = dimension//尺寸、重量
      this.pram.id = id
      this.pram.number = number
    },
    addTreeListLabelForCasCard(arr, child) {
      arr.forEach((o, i) => {
        if (o.child && o.child.length) {
          // o.disabled = true
          o.child.forEach((j) => {
            //j.disabled = true
          })
        }
      })
    },
    handlerSubmit: Debounce(function(form) {
      this.$refs[form].validate(valid => {
        if (!valid) return
        if (this.isCreate === 0) {
          this.handlerSave()
        } else {
          this.handlerEdit()
        }
      })
    }),
    handlerSave() {
      modelTypeApi.typeAdd(this.pram).then(data => {
        this.$message.success('创建设备成功')
        this.$emit('hideEditDialog')
      })
    },
    handlerEdit() {
      modelTypeApi.typeAdd(this.pram).then(data => {
        this.$message.success('更新设备成功')
        this.$emit('hideEditDialog')
      })
    }
  }
}
</script>

<style scoped>

</style>
